package com.lishe.lishe.lishe.module.myorder.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lishe.lishe.R;
import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.lishe.common.CommonUtils;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.myorder.bean.BusinessObject;
import com.lishe.lishe.lishe.module.myorder.bean.GoodObject;
import com.lishe.lishe.lishe.module.myorder.bean.OrderObject;
import com.lishe.lishe.lishe.widget.ConfirmDialog;

import java.util.List;


/**
 * Created by lwa on 2017/8/11.
 * 订单对象
 */


public class OrderAdapter extends BaseAdapter {
    private Context context;
    private String[] orderType;
    private String[] orderBtnType;
    private List<OrderObject> lisOrders;
    private ImageLoaderUtils imageLoaderUtils;
    private ConfirmDialog.Builder callPhoneDialog;


    public OrderAdapter(Context context, List<OrderObject> lisOrders) {
        this.context = context;
        this.lisOrders = lisOrders;
        orderType = context.getResources().getStringArray(R.array.order_type);
        orderBtnType = context.getResources().getStringArray(R.array.order_btn_type);
        imageLoaderUtils = new ImageLoaderUtils();
        createCallPhone();
    }

    @Override
    public int getCount() {
        return lisOrders == null ? 0 : lisOrders.size();
    }

    @Override
    public Object getItem(int i) {
        return lisOrders.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    String orderStatus;//订单状态
    String windowMsg;//弹窗内容

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        final OrderObject order = lisOrders.get(position);
        OrderAdapterViewHolder orderHolder;
        if (view == null) {
            view = View.inflate(context, R.layout.item_order_adapter, null);
            orderHolder = new OrderAdapterViewHolder(view);
            view.setTag(orderHolder);
        } else {
            orderHolder = (OrderAdapterViewHolder) view.getTag();
        }
        orderStatus = order.getStatusToString();
        switch (orderStatus) {
            case "待付款":
                orderHolder.btnCancle.setVisibility(View.VISIBLE);
                orderHolder.btnPay.setVisibility(View.VISIBLE);
                orderHolder.btnCancle.setText(orderBtnType[0]);
                orderHolder.btnPay.setText(orderBtnType[4]);
                windowMsg = context.getString(R.string.iscancle_order);
                break;
            case "已取消":
            case "已完成":
            case "交易成功":
                orderHolder.btnCancle.setVisibility(View.GONE);
                orderHolder.btnPay.setVisibility(View.GONE);
                break;
            case "待发货":
            case "已付款":
                orderHolder.btnCancle.setVisibility(View.VISIBLE);
                orderHolder.btnPay.setVisibility(View.GONE);
                orderHolder.btnCancle.setText(orderBtnType[1]);
                break;
            case "待收货"://已付款
            case "卖家已发货":
                orderHolder.btnCancle.setVisibility(View.GONE);
                orderHolder.btnPay.setVisibility(View.VISIBLE);
                orderHolder.btnPay.setText(orderBtnType[3]);
                windowMsg = context.getString(R.string.isreceive_order);
                break;
            case "退换货":
                orderHolder.btnCancle.setVisibility(View.GONE);
                orderHolder.btnPay.setVisibility(View.GONE);
                break;

        }

        BusinessViewHolder businessHolder;
        orderHolder.businessLayout.removeAllViews();
        orderHolder.orderTotalMoney.setText((int)(order.getMoney()*100) + "");
        orderHolder.QQ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtils.gotoQQ(context, Globals.LISHE_QQ);
            }
        });
        orderHolder.phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                callPhoneDialog.show();
            }
        });
        orderHolder.btnCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (buttonOnclikListener != null) {
                    buttonOnclikListener.graButtonLitener(order, position);
                }
            }
        });
        orderHolder.btnPay.setOnClickListener(new View.OnClickListener() {//立即付款
            @Override
            public void onClick(View view) {
                if (buttonOnclikListener != null) {
                    buttonOnclikListener.redButtonLitener(order, position);
                }
            }
        });

        for (int j = 0; j < order.getTrade_paybillData().size(); j++) {
            BusinessObject business = order.getTrade_paybillData().get(j);
            View vj = View.inflate(context, R.layout.item_order_adapter_business, null);
            businessHolder = new BusinessViewHolder(vj);
            businessHolder.businessName.setText(business.getTradeData().getShopName());
            businessHolder.businessType.setText(business.getTradeData().getStatusToString());
            if (business.getTradeData().getPost_fee() != 0) {
                businessHolder.businessTotalMoney.setText(String.format(context.getString(R.string.business_total_money_postfee), (int)(business.getPayment()*100) + "", (int)(business.getTradeData().getPost_fee()) + ""));
            } else {
                businessHolder.businessTotalMoney.setText(String.format(context.getString(R.string.business_total_money),(int) (business.getPayment()*100) + ""));
            }
            orderHolder.businessLayout.addView(vj);
            GoodsViewHolder goodsHolder;
            for (int i = 0; i < business.getTradeData().getOrderList().size(); i++) {
                View vi = View.inflate(context, R.layout.item_order_adapter_business_good, null);
                goodsHolder = new GoodsViewHolder(vi);
                GoodObject good = business.getTradeData().getOrderList().get(i);
                goodsHolder.goodsCount.setText("x" + good.getNum());
                goodsHolder.goodsDescribe.setText(good.getTitle());
                goodsHolder.goodsFormat.setText(good.getSpec_info());
                goodsHolder.goodsPrice.setText((int)(good.getPrice()*100) + "");
                imageLoaderUtils.displayImage(good.getPic_path(), goodsHolder.goodsImg);
                businessHolder.goodsLayout.addView(vi);
            }
        }
        return view;
    }


    /**
     * 订单holder
     */
    class OrderAdapterViewHolder {

        public LinearLayout businessLayout;
        private ImageView QQ;
        private ImageView phone;
        private Button btnPay;
        private Button btnCancle;
        private TextView orderTotalMoney;

        OrderAdapterViewHolder(View v) {
            businessLayout = v.findViewById(R.id.business_layout);
            orderTotalMoney = v.findViewById(R.id.pay_money);
            QQ = v.findViewById(R.id.order_qq_img);
            phone = v.findViewById(R.id.order_phone_img);
            btnPay = v.findViewById(R.id.pay_button);
            btnCancle = v.findViewById(R.id.cancle_button);
        }
    }

    /**
     * 商铺holder
     */
    class BusinessViewHolder {

        public TextView businessType;
        public TextView businessName;
        public TextView businessTotalMoney;
        public LinearLayout goodsLayout;

        BusinessViewHolder(View v) {
            businessName = v.findViewById(R.id.business_name);
            businessType = v.findViewById(R.id.business_type);
            goodsLayout = v.findViewById(R.id.goods_layout);
            businessTotalMoney = v.findViewById(R.id.business_total_money);
        }
    }

    /**
     * 商品holder
     */
    class GoodsViewHolder {
        public ImageView goodsImg;
        public TextView goodsDescribe;
        public TextView goodsFormat;
        public TextView goodsPrice;
        public TextView goodsCount;

        GoodsViewHolder(View v) {
            goodsImg = v.findViewById(R.id.goods_img);
            goodsDescribe = v.findViewById(R.id.goods_describe);
            goodsFormat = v.findViewById(R.id.goods_format);
            goodsPrice = v.findViewById(R.id.goods_price);
            goodsCount = v.findViewById(R.id.goods_count);
        }
    }

    /**
     * 拨号
     */
    private void createCallPhone() {
        callPhoneDialog = new ConfirmDialog.Builder(context);
        callPhoneDialog.setContent(Globals.LISHE_PHONE);
        callPhoneDialog.setContentGravity(Gravity.CENTER_HORIZONTAL);
        callPhoneDialog.setPositiveButton("确定", new ConfirmDialog.Builder.OnPositiveListener() {
            @Override
            public void onPositive(ConfirmDialog dialog) {
                dialog.dismiss();
                CommonUtils.callPhone((Activity) context, Globals.LISHE_PHONE);
            }
        });
    }

    /**
     * 按钮的回调
     */
    ButtonOnclikListener buttonOnclikListener;

    public interface ButtonOnclikListener {
        void redButtonLitener(OrderObject orderObject, int position);

        void graButtonLitener(OrderObject orderObject, int position);
    }

    public void setOnButtonListener(ButtonOnclikListener buttonOnclikListener) {
        this.buttonOnclikListener = buttonOnclikListener;
    }
}
